<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
    <a-form layout="inline">
    <a-row >
      <a-col :md="4" :sm="8" style="margin-left: 20px">
        <a-button type="primary" @click="handleAdd(1)">创建一级部门/组织</a-button>
      </a-col>
    </a-row>
    </a-form>
    </div>
    <a-divider orientation="left" style="margin-top: 25px">
      部门/组织
    </a-divider>
    <a-row>

      <!-- 树-->
      <a-col :md="10" :sm="24">
        <template>
          <a-dropdown :trigger="[this.dropTrigger]" @visibleChange="dropStatus">
                <span style="user-select: none">
                  <a-tree
                    :defaultExpandAll="true"
                    :selectedKeys="selectedKeys"
                    :checkedKeys="checkedKeys"
                    :checkStrictly="true"
                    @select="onSelect"
                    @check="onCheck"
                    @rightClick="rightHandle"
                    @expand="onExpand"
                    v-if="treeData.length"
                    :tree-data="treeData"
                    :default-expanded-keys="this.keyId"
                  />
                </span>
            <!--新增右键点击事件,和增加添加和删除功能-->
            <a-menu slot="overlay">
              <!--            <a-menu-item v-show="orgType == 0 || orgType == 1" @click="handleAdd(3)" key="1"-->
              <!--            >添加子架构-->
              <!--            </a-menu-item-->
              <!--            >-->
              <!--            <a-menu-item v-show="orgType == 1 || orgType == 2" @click="handleAdd(4)" key="4"-->
              <!--            >添加同级架构-->
              <!--            </a-menu-item-->
              <!--            >-->
              <a-menu-item @click="handleAdd(2)" key="5">新增子部门</a-menu-item>
              <a-menu-item @click="handleAdd(3)" key="6">重命名</a-menu-item>
              <!--            <a-menu-item v-show="orgType != 3" @click="handleAdd(7)" key="7">修改</a-menu-item>-->
              <a-menu-item >
                <a-popconfirm title="确定删除吗?" @confirm="() => deleteDepartmentById()">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
              <!--            <a-menu-item v-show="orgType == 3">-->
              <!--              <a-popconfirm title="确定解绑吗?" @confirm="() => handleDelete()">-->
              <!--                <a>解绑</a>-->
              <!--              </a-popconfirm>-->
              <!--            </a-menu-item>-->
                          <a-menu-item @click="closeDrop" key="3">取消</a-menu-item>
            </a-menu>
          </a-dropdown>
        </template>
      </a-col>
    </a-row>

    <!--    <depart-modal ref="departModal" @ok="newLoadTree"></depart-modal>-->
    <!--    <depart-modal2 ref="departModal2" @ok="newLoadTree"></depart-modal2>-->

    <!--    &lt;!&ndash; 表单区域 &ndash;&gt;-->
    <monthlyDepartment-modal ref="modalForm" @ok="modalFormOk"></monthlyDepartment-modal>


  </a-card>
</template>

<script>
import MonthlyDepartmentModal from './modules/MonthlyDepartmentModal'
import {JeecgListMixin} from '@/mixins/JeecgListMixin'
import {deleteAction, postAction} from '@/api/manage'

export default {
  name: "MonthlyDepartmentList",
  mixins: [JeecgListMixin],
  components: {
    MonthlyDepartmentModal
  },
  data() {
    return {
      description: '月卡车部门组织表管理页面',
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: "center",
          customRender: function (t, r, index) {
            return parseInt(index) + 1;
          }
        },
        {
          title: '父id',
          align: "center",
          dataIndex: 'parentId'
        },
        {
          title: '部门名称',
          align: "center",
          dataIndex: 'departName'
        },
        {
          title: '停车场编号',
          align: "center",
          dataIndex: 'parkCode'
        },
        {
          title: '停车场名称',
          align: "center",
          dataIndex: 'parkName'
        },
        {
          title: '部门类型(0:一级部门, 1:二级部门,2:三级部门)',
          align: "center",
          dataIndex: 'departType'
        },
        {
          title: '部门排序',
          align: "center",
          dataIndex: 'sortNo'
        },
        {
          title: '创建人',
          align: "center",
          dataIndex: 'createBy'
        },
        {
          title: '更新人',
          align: "center",
          dataIndex: 'updateBy'
        },
        {
          title: '更新时间',
          align: "center",
          dataIndex: 'updateTime'
        },
        {
          title: '创建时间',
          align: "center",
          dataIndex: 'createTime'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: "center",
          scopedSlots: {customRender: 'action'},
        }
      ],
      url: {
        queryTreeList: "/monthlyDepartment/queryTreeList",
        addDepartment: "/monthlyDepartment/addDepartment",
        queryById:"/monthlyDepartment/queryById",
        delete: "/monthlyDepartment/delete",
        deleteBatch: "/monthlyDepartment/deleteBatch",
        exportXlsUrl: "/monthlyDepartment/exportXls",
        importExcelUrl: "/monthlyDepartment/importExcel",
      },
      queryParam: {
        "opTime_begin": "",
        "opTime_end": "",
        "type": "-1",
        "parkCode":""

      },
      isorter: {
        column: 'createTime',
        order: 'desc',
      },
      dropTrigger: '',
      checkedKeys: [],
      selectedKeys: [],
      keyId: [],
      treeData: [],
      orgType: 0,


    }
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
    }
  },
  created() {
    this.keyupEnter()
    //this.loadParkInfo()
    this.loadTree()
  },
  methods: {
    deleteDepartmentById(){
      var id=this.selectedKeys[0];
      postAction(this.url.delete,{id:id}).then((res)=>{
        if(res.success) {
            this.$message.success("删除成功");
            this.loading = false
            this.loadTree();
        }else {
          this.$message.warning(res.message);
          this.loading = false
        }
      })
    },
    modalFormOk(){
      this.loadTree()
    },

    loadTree() {
      const that = this
      postAction(this.url.queryTreeList).then((res) => {
        if (res.success) {
          that.treeData = res.result;
          this.loading = false
        }
      })
    },
    newLoadTree() {
      this.treeData = []
      this.loadTree()
    },
    handleAdd: function (num) {
      if (1 === num) {
        this.$refs.modalForm.title = "新增一级部门/组织";
        this.$refs.modalForm.add(num, this.selectedKeys, this.queryParam.parkCode);
      }
      if (3 === num) {
        this.$refs.modalForm.title = "重命名";
        this.$refs.modalForm.add(num, this.selectedKeys, this.queryParam.parkCode);
      }
      if (2 === num) {
        //判断当前菜单是否是最小级部门，如果是最小级别，则不再允许添加子部门
        postAction(this.url.queryById,{id:this.selectedKeys[0]}).then((res)=>{
          if(res.success){
            if(res.result){
              if(res.result.departType=='2'){
                //是最小级别
                this.$message.warning("该部门下不可再添加子部门");
              }else{
                this.$refs.modalForm.title = "新增子部门/组织";
                this.$refs.modalForm.add(num, this.selectedKeys, this.queryParam.parkCode);
              }
            }
          }
        })
      }
    },
    onSelect(selectedKeys, e) {
      this.hiding = false
      let record = e.node.dataRef
      this.selectedKeys = [record.key];
    },
    onCheck(checkedKeys, info) {
      this.hiding = false
      this.checkedKeys = checkedKeys.checked
    },
    // 右键操作方法
    rightHandle(node) {
      window.localStorage.setItem('deptId', node.node.dataRef.id);
      this.dropTrigger = 'contextmenu';
      this.orgType = node.node.dataRef.orgType;
      this.departName = node.node.dataRef.departName;
      this.rightClickSelectedKey = node.node.eventKey;
      this.selectedKeys=[node.node.dataRef.key];
    },

    onExpand(expandedKeys) {
      this.iExpandedKeys = expandedKeys
    },
    // 右键点击下拉框改变事件
    dropStatus(visible) {
      if (visible == false) {
        this.dropTrigger = ''
      }
    },
    // 右键店家下拉关闭下拉框
    closeDrop() {
      this.dropTrigger = ''
    },

    batchDel: function () {
      if (this.checkedKeys.length <= 0) {
        this.$message.warning('请选择一条记录！')
      } else {
        var ids = ''
        for (var a = 0; a < this.checkedKeys.length; a++) {
          ids += this.checkedKeys[a] + ','
        }
        var that = this
        this.$confirm({
          title: '确认删除',
          content: '确定要删除所选中的 ' + this.checkedKeys.length + ' 条数据?',
          onOk: function () {
            deleteAction(that.url.deleteBatch, {ids: ids}).then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.loadTree()
                that.onClearSelected()
              } else {
                that.$message.warning(res.message)
              }
            })
          },
        })
      }
    },
    keyupEnter() {
      document.onkeydown = e => {
        let body = document.getElementsByTagName('body')[0]
        if (e.keyCode === 13) {
          this.loadData(1);
        }
      }
    },

  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>